package com.feijiang.controller;


import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.feijiang.entity.ExpeRecord;
import com.feijiang.service.IExpeRecordService;
import com.feijiang.util.ResponseData;
import javax.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-28
 */
@RestController
@RequestMapping("/expe-record")
public class ExpeRecordController {

  @Resource
  IExpeRecordService iExpeRecordService;

  @PostMapping("/add")
  public ResponseData add(@RequestBody ExpeRecord expeRecord) {
    Assert.isTrue(expeRecord.getExpeId() != null, "没有选择实验ID");
    return ResponseData.success("添加成功", iExpeRecordService.add(expeRecord));
  }

  @PostMapping("/edit")
  public ResponseData edit(@RequestBody ExpeRecord expeRecord) {
    Assert.isTrue(expeRecord.getId() != null, "缺少id");
    return ResponseData.success("修改成功", iExpeRecordService.edit(expeRecord));
  }

  @PostMapping("/delete/{id}")
  public ResponseData delete(@PathVariable("id") int id) {
    Assert.isTrue(iExpeRecordService.deleteById(id), "删除失败");
    return ResponseData.success("删除成功", true);
  }

  @GetMapping("/list")
  public ResponseData queryByPage(@RequestParam(defaultValue = "10") int pageSize,
      @RequestParam(defaultValue = "1") int pageNum,@RequestParam(required = true) int expeId) {
    Assert.isTrue(pageNum > 0 && pageSize > 0, "请输入正确的参数");
    return ResponseData.success(iExpeRecordService.queryByPage(pageSize, pageNum, expeId));
  }

  @GetMapping("/listAllByPage")
  public ResponseData listAllByPage(@RequestParam(defaultValue = "10") int pageSize,
      @RequestParam(defaultValue = "1") int pageNum) {
    return ResponseData.success(iExpeRecordService.listAllByPage(pageSize, pageNum));
  }

  @GetMapping("/{id}")
  public ResponseData queryById(@PathVariable("id") int id) {
    return ResponseData.success(iExpeRecordService.getById(id));
  }

}
